package pack1;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.swim.entity.Collaboration;
import com.swim.session.CollaborationRemote;

public class CollaborationServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
      
    
    public CollaborationServlet() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    	HttpSession session = request.getSession();
    	if(session.getAttribute("authenticated") == null || !(boolean)session.getAttribute("authenticated")) {
    		response.sendRedirect("index.jsp");
			return;
    	}

    	List<Collaboration> collaborationsOffered = getOfferedCollaborations((String) session.getAttribute("authenticated-email"));
    	List<Collaboration> collaborationsReceived = getReceivedCollaborations((String) session.getAttribute("authenticated-email"));
    	request.setAttribute("collaborationsOffered", collaborationsOffered);
    	request.setAttribute("collaborationsReceived", collaborationsReceived);
    	
		RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/auth/collaboration.jsp");
		dispatcher.forward(request, response);
    }
    
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	if(request.getSession().getAttribute("authenticated") == null || !(boolean)request.getSession().getAttribute("authenticated")) {
    		response.sendRedirect("index.jsp");
			return;
    	}
    	HttpSession session = request.getSession();
    	
    	List<Collaboration> collaborationsOffered = getOfferedCollaborations((String) session.getAttribute("authenticated-email"));
    	List<Collaboration> collaborationsReceived = getReceivedCollaborations((String) session.getAttribute("authenticated-email"));
    	request.setAttribute("collaborationsOffered", collaborationsOffered);
    	request.setAttribute("collaborationsReceived", collaborationsReceived);
    	
		RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/auth/collaboration.jsp");
		dispatcher.forward(request, response);
		
    }

	private List<Collaboration> getOfferedCollaborations(String email) {
		try {
			
			InitialContext jndiContext = (InitialContext) Util.getInitialContext();
			Object ref = jndiContext.lookup("CollaborationBeanJNDI");
			CollaborationRemote r = (CollaborationRemote) ref;
			
			List<Collaboration> results = new ArrayList<Collaboration>();
			results.addAll(r.getOfferedCollaborations(email));
			return results;
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	private List<Collaboration> getReceivedCollaborations(String email) {
		try {
			
			InitialContext jndiContext = (InitialContext) Util.getInitialContext();
			Object ref = jndiContext.lookup("CollaborationBeanJNDI");
			CollaborationRemote r = (CollaborationRemote) ref;
			
			List<Collaboration> results = new ArrayList<Collaboration>();
			results.addAll(r.getReceivedCollaborations(email));
			return results;
		} catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
}